﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ProgectEdit.aspx.cs" Inherits="HQWeb.Progect.ProgectManage.ProgectEdit" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<base target="_self" />
    <title></title>
    <link href="../../Styles/Site.css" rel="Stylesheet" type="text/css" />
    <style type="text/css">
        #b1
        {
            width: 431px;
        }
    </style>
    <script type="text/javascript" language="javascript">
        //添加元素表格
        function AddTable() {
            var Count = document.getElementById("hidCount").value;
            var strHtml = "";
            strHtml = strHtml + '  <table width="90%" id=\"table' + Count + '\">';
            strHtml = strHtml + " <hr />";
            strHtml = strHtml + " <tr>";
            strHtml = strHtml + '<td align="right" style="width:20%">';
            strHtml = strHtml + "元素名称：";
            strHtml = strHtml + "</td>";
            strHtml = strHtml + "<td align=\"left\">";
            strHtml = strHtml + " <input type=\"text\" this.fireEvent('onchange')  readonly=\"readonly\" onchange=\"this.setAttribute('value',this.value);\" id=\"y" + Count + "\" /><input type=\"button\" id=\"btny" + Count + "\" value=\"选择\"  onclick=\"openTreeSelect('d" + Count + "','y" + Count + "','')\" />";
            strHtml = strHtml + " <input type=\"text\" onchange=\"this.setAttribute('value',this.value);\" id=\"d" + Count + "\" style=\"display: none\" />";
            strHtml = strHtml + " <input type=\"text\" onchange=\"this.setAttribute('value',this.value);\" id=\"e" + Count + "\" style=\"display: none\" />";
            strHtml = strHtml + "</td>";
            strHtml = strHtml + "</tr>";
            strHtml = strHtml + "<tr>";
            strHtml = strHtml + "<td align=\"right\">";
            strHtml = strHtml + "数量：";
            strHtml = strHtml + "</td>";
            strHtml = strHtml + "<td align=\"left\">";
            strHtml = strHtml + "<input onchange=\"this.setAttribute('value',this.value);\" type=\"text\" id=\"s" + Count + "\" />";
            strHtml = strHtml + " </td>";
            strHtml = strHtml + " </tr>";
            strHtml = strHtml + "<tr>";
            strHtml = strHtml + " <td align=\"right\">";
            strHtml = strHtml + "说明：";
            strHtml = strHtml + "</td>";
            strHtml = strHtml + "<td align=\"left\" colspan=\"2\">";
            strHtml = strHtml + "<input onchange=\"this.setAttribute('value',this.value);\" type=\"text\" id=\"b" + Count + "\"  style=\"width: 400px\"/>";
            strHtml = strHtml + "</td>";
            strHtml = strHtml + "</tr>";
            strHtml = strHtml + "<tr align=\"left\">";
            strHtml = strHtml + "<td colspan=\"4\">";
            strHtml = strHtml + "<input onchange=\"this.setAttribute('value',this.value);\" type=\"button\" id=\"del" + Count + "\" value=\"删除该元素\"  onclick=\"DeleteNode('table" + Count + "')\"/>";
            strHtml = strHtml + "</td>";
            strHtml = strHtml + " </tr>";

            strHtml = strHtml + " </table>";

            document.getElementById("hidCount").value = parseInt(Count, 10) + 1;
            document.getElementById("Addtable").innerHTML = document.getElementById("Addtable").innerHTML + strHtml;
        }
        //JS 删除节点
        function DeleteNode(nodeID) {
            var varType = document.getElementById("hidType").value;
            if (varType == "edit") {
                var varDelID = "e" + nodeID.substr(5);
                varDelID = document.getElementById(varDelID).value;
                HQWeb.Progect.ProgectManage.ProgectServer.DelprogectY(varDelID, DelResout, failedCallback);
            }
            var trnode = document.getElementById(nodeID);
            trnode.parentNode.removeChild(trnode);

        }
        //删除回调函数
        function DelResout(resout) {
            if (resout == "") {
            }
            else {
                if (resout == "SessionTimeout") {
                    top.location.href = '../../Login/Login.aspx';
                }
                else {
                    alert("resout");
                }
            }
        }

        function checkRate(input) {
            var value = input.value;
            if (value != "") {
                var re = /^[0-9]+.?[0-9]*$/;
                if (!re.test(value)) {
                    alert("请输入数字(例:0.02)");
                    input.value = "";
                    input.focus();
                    return false;
                }
            } else {
                //                alert("价格不能为空");
                //                input.focus();
            }

        }
        //保存事情
        function AddData() {
            document.getElementById("btnAdd").disabled = true;     //不可保存
            var nameInput = document.getElementById("txtName");
            if (nameInput.value == "") {
                alert("项目名称不能为空！");
                nameInput.focus();
                document.getElementById("btnAdd").disabled = false;     //可保存
                return;
            }
            var varType = document.getElementById("hidType").value;
            var ProgectModel = new Object();
            if (varType == "edit") {
                ProgectModel.PROJECT_ID = document.getElementById("hidPid").value;
            }
            ProgectModel.PROJECT_NAME = nameInput.value;
            ProgectModel.PRICE = document.getElementById("txtPrice").value;
            ProgectModel.REMARK = document.getElementById("txtRemark").value;
            ProgectModel.BUDGET = document.getElementById("txtBudGet").value;
            var dropTemp = document.getElementById("dropBranch");
            if (dropTemp.options[dropTemp.selectedIndex].value != '<%=Session["BRANCH_ID"]%>') {
                var flag = confirm("项目所属分店与您所属的分店不相同！是否添加？");
            }
            if (flag == undefined || flag) {
                ProgectModel.BRANCH_ID = dropTemp.options[dropTemp.selectedIndex].value;
                dropTemp = document.getElementById("dropType");
                ProgectModel.PROJECTTYPE_ID = dropTemp.options[dropTemp.selectedIndex].value;
                var info = GeArrList(varType);
                if (info[0] != "") {
                    alert(info[0]);
                    document.getElementById("btnAdd").disabled = false;     //可保存
                    return;
                } else {
                    if (varType == "edit") {
                        HQWeb.Progect.ProgectManage.ProgectServer.Edit(ProgectModel, info[1], SaveResout, failedCallback);
                    } else {
                        HQWeb.Progect.ProgectManage.ProgectServer.Add(ProgectModel, info[1], SaveResout, failedCallback);
                    }
                }
                document.getElementById("btnAdd").disabled = false;     //可保存
            }
            else {
                document.getElementById("btnAdd").disabled = false;
            }
        }
        //保存返回结果
        function SaveResout(resout) {
            if (resout == "") {
                alert("保存成功！");
                Goback();
            } else {
            if (resout == "SessionTimeout") {
                top.location.href = '../../Login/Login.aspx';
            }
            else {
                alert(resout);
            }
            }
        }
        //获得元素事件
        function GeArrList(type) {
            var objArry = new Array();
            var Count = document.getElementById("hidCount").value;
            var varPID = document.getElementById("hidPid").value;
            for (var i = 0; i < Count; i++) {
                var idTemp = "d" + i;
                var inputTpem = document.getElementById(idTemp);
                if (inputTpem != null && inputTpem != undefined) {
                    var obj = new Object();
                    if (inputTpem.value == "") {
                        var alter = "元素种类必填！";
                        return [alter, null];
                    } else {
                        obj.MATERIAL_ID = inputTpem.value;
                        idTemp = "s" + i;
                        inputTpem = document.getElementById(idTemp);
                        if (inputTpem.value != "") {
                            obj.NUM = inputTpem.value;
                        }
                        idTemp = "b" + i;
                        obj.REMARK = document.getElementById(idTemp).value;
                        if (type == "edit") {
                            idTemp = "e" + i;
                            if (document.getElementById(idTemp).value != "") {
                                obj.ID = document.getElementById(idTemp).value;
                            } else {
                                obj.ID = 0;
                            }

                            obj.PROJECT_ID = varPID;
                        }
                    }
                    objArry.push(obj);
                }

            }
            return ["", objArry]
        }
        //选择
        function openTreeSelect(vObjID, vObjName, vObjParam) {
            var vURL = '../../Resources/SelectFrame.aspx?MaterialID=' + vObjID + '&MaterialName=' + vObjName + '&Param=' + vObjParam;
            //vURL = vURL + '&objname=' + vObjName + '&objid=' + vObjID;
            window.open(vURL, "", "top=150px,left=200px,height=600px,width=800px");
           
        }

        window.onload = function () {
            if (document.getElementById("hidType").value == "edit") {
                var strID = document.getElementById("hidPid").value;
                HQWeb.Progect.ProgectManage.ProgectServer.progetModel(strID, GetResout, failedCallback);
            }
        }
        function GetResout(obj) {
            if (obj != null) {
                var nameInput = document.getElementById("txtName");
                nameInput.value = obj.PROJECT_NAME;
                if (obj.PRICE != undefined && obj.PRICE != "") {
                    document.getElementById("txtPrice").value = obj.PRICE.toString();
                }
                document.getElementById("txtRemark").value = obj.REMARK;
                document.getElementById("txtBudGet").value = obj.BUDGET;
//                var dropTemp = document.getElementById("dropBranch");
//                dropTemp.options[dropTemp.selectedIndex].value = obj.BRANCH_ID;
                document.getElementById("dropBranch").value = obj.BRANCH_ID;
                dropTemp = document.getElementById("dropType");
                dropTemp.options[dropTemp.selectedIndex].value = obj.PROJECTTYPE_ID;
            }
        }
        //客户端异常处理
        function failedCallback(error) {
            alert(error.get_message());
        }
        function Goback() {
            var from = document.getElementById("hidFrom").value;
            if (from == "close") {
                window.opener.document.getElementById("bnSearch").click();
                window.close();
            } else {
                var dropTemp = document.getElementById("dropType");
                var varID = dropTemp.options[dropTemp.selectedIndex].value;
                location = "MangerList.aspx?id=" + varID;
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div align="center">
            <asp:ScriptManager ID="ScriptManager1" runat="server">
                <Services>
                    <asp:ServiceReference Path="ProgectServer.asmx" />
                </Services>
            </asp:ScriptManager>
            <table width="98%">
                <tr>
                    <td colspan="4" class="title">
                        <asp:Label runat="server" ID="labTitle" Text="添加项目"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td align="right">
                        服务项目名称：
                    </td>
                    <td align="left">
                        <input type="text" id="txtName" maxlength="100" />
                    </td>
                    <td align="right">
                        价 格：
                    </td>
                    <td align="left">
                        <input type="text" id="txtPrice" maxlength="100" onblur="checkRate(this)" />&nbsp;元
                    </td>
                </tr>
                <tr>
                    <td align="right">
                        所属店：
                    </td>
                    <td align="left">
                        <div runat="server" id="div1" style="display: none">
                            <asp:DropDownList runat="server" ID="dropBranch">
                            </asp:DropDownList>
                        </div>
                        <div runat="server" id="div2" style="display: none">
                            <asp:Label runat="server" ID="labBranch"></asp:Label>
                        </div>
                    </td>
                    <td align="right">
                        所属分类：
                    </td>
                    <td align="left">
                        <asp:DropDownList runat="server" ID="dropType">
                        </asp:DropDownList>
                    </td>
                </tr>
                <tr>
                    <td align="right">
                        婚礼预算：
                    </td>
                    <td colspan="3" align="left">
                        <input type="text" id="txtBudGet" maxlength="8" onblur="checkRate(this)" />&nbsp;元
                    </td>
                </tr>
                <tr>
                    <td align="right">
                        说明：
                    </td>
                    <td colspan="3" align="left">
                        <input type="text" id="txtRemark" maxlength="200" style="width: 400px" />
                    </td>
                </tr>
                <tr>
                    <td colspan="4">
                        <div align="center">
                            <div style="text-align: left; padding-left: 50px; padding-top: 5px;">
                                <input type="button" value="添加元素" id="add1" onclick="AddTable()" />
                            </div>
                            <div id="Addtable" runat="server">
                            </div>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td colspan="4">
                        <input type="button" value="保 存" onclick="AddData()" id="btnAdd" />&nbsp;&nbsp;
                        <input type="button" value="返 回" onclick=" Goback()" />
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <div style="display: none">
        <asp:HiddenField ID="hidPid" runat="server" />
        <asp:HiddenField ID="hidType" runat="server" Value="add" />
        <asp:HiddenField ID="hidCount" runat="server" Value="0" />
          <asp:HiddenField ID="hidFrom" runat="server" Value="new" />
    </div>
    </form>
</body>
</html>
